package com.mmg.spzx.manager.controller;

import com.mmg.spzx.common.log.annotation.Log;
import com.mmg.spzx.manager.service.SysMenuService;
import com.mmg.spzx.manager.service.SysUserService;
import com.mmg.spzx.manager.service.ValidateCodeService;
import com.mmg.spzx.model.dto.system.LoginDto;
import com.mmg.spzx.model.entity.system.SysUser;
import com.mmg.spzx.model.vo.common.Result;
import com.mmg.spzx.model.vo.common.ResultCodeEnum;
import com.mmg.spzx.model.vo.system.LoginVo;
import com.mmg.spzx.model.vo.system.SysMenuVo;
import com.mmg.spzx.model.vo.system.ValidateCodeVo;
import com.mmg.spzx.utils.AuthContextUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "用户接口")
@RequestMapping("/admin/system/index")
@RestController
public class IndexController {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private ValidateCodeService validateCodeService;

    @Autowired
    private SysMenuService sysMenuService;

    @Operation(summary = "查询用户可以操作的菜单")
    @GetMapping(value = "/menus")
    public Result menus() {
        List<SysMenuVo> list = sysMenuService.findMenusByUserId(AuthContextUtil.get().getId());
        return Result.build(list, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "用户退出")
    @GetMapping(value = "/logout")
    public Result logout(@RequestHeader(value = "token") String token) {
        sysUserService.logout(token);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "获取用户信息")
    @GetMapping(value = "/getUserInfo")
    public Result<SysUser> getUserInfo(@RequestHeader(name = "token") String token) {
        //SysUser sysUser = sysUserService.getUserInfo(token);
        SysUser sysUser = AuthContextUtil.get();
        return Result.build(sysUser, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "获取验证码")
    @GetMapping(value = "/generateValidateCode")
    public Result<ValidateCodeVo> generateValidateCode() {
        ValidateCodeVo validateCodeVo = validateCodeService.generateValidateCode();
        return Result.build(validateCodeVo, ResultCodeEnum.SUCCESS);
    }


    @Log(title = "用户登录", businessType = 0)
    @Operation(summary = "用户登录")
    @PostMapping("/login")
    public Result login(@RequestBody LoginDto loginDto) {
        LoginVo loginVo = sysUserService.login(loginDto);
        return Result.build(loginVo, ResultCodeEnum.SUCCESS);
    }
}
